//
//  FJFeedbackViewController.swift
//  FruitJuice
//
//  Created by Rui on 2023/12/16.
//

import UIKit

class FJFeedbackViewController: FJBaseViewController {

    private let backButton          = c_make_ex(UIButton.self)
        .image("FJ_Profile_back").end()
    private let titleLabel          = c_make_ex(UILabel.self)
        .text("Feedback").color(.black).font(19.font_bold).end()
    private let iconImageView       = c_make_ex(UIImageView.self)
        .image("FJ_Profile_Feedback_icon").mode(.scaleAspectFill).end()
    private let desLabel            = c_make_ex(UILabel.self)
        .text("If you have any questions, you can contact Customer service email, we will get in touch with you as soon as possible.").color(.black).font(18.font_regular)
        .line(0).alignment(.center).end()
    private lazy var emailTextField = c_make_ex(UITextField.self)
        .bgColor("#EFEFEF").corner(25.0)
        .placeholder("Please enter your email address")
        .font(17.font_medium).color(.black).end()
    private lazy var textView       = c_make_ex(UITextView.self)
        .bgColor("#EFEFEF").corner(20.0).placeholderFont(17.font_medium)
        .color(.black).placeholderColor("#B5B5B5").font(17.font_medium)
        .placeholder("Describe the problem you encountered in detail so that we can deal with it as soon as possible.")
        .placeholderInsets(UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)).end()
    private lazy var submitButton = c_make_ex(UIButton.self)
        .bgColor("#FF9133").corner(25.0).text("Submit")
        .font(21.font_bold).color(.white).end()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.isHidden = true
        view.subviews{
            backButton
            titleLabel
            iconImageView
            desLabel
            emailTextField
            textView
            submitButton
        }
        
        backButton.top(.statusHeight + 7).leading(15).size(30)
        titleLabel.leading(60)
        titleLabel.CenterY == backButton.CenterY
        iconImageView.size(145).centerHorizontally()
        iconImageView.Top == backButton.Bottom + 30
        desLabel.leading(43.5).trailing(43.5)
        desLabel.Top == iconImageView.Bottom + 5
        emailTextField.leading(27.5).trailing(27.5).height(50)
        emailTextField.Top == desLabel.Bottom + 20
        textView.leading(27.5).trailing(27.5).height(170)
        textView.Top == emailTextField.Bottom + 15
        submitButton.leading(27.5).trailing(27.5).height(50).bottom(85)

        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        submitButton.addTarget(self, action: #selector(clickSubmitButton), for: .touchUpInside)
        emailTextField.style { v in
            v.placehold("#B5B5B5".toColor, font: 17.font_medium)
            let leftV = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
            let leftImg = UIImageView(image: "FJ_Profile_Feedback_email".toImage)
            leftImg.frame = CGRect(x: 15, y: 13, width: 24, height: 24)
            leftV.addSubview(leftImg)
            v.leftView = leftV
            v.leftViewMode = .always
        }
        textView.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    @objc private func clickSubmitButton(){
        guard let email = emailTextField.text, email.isValidEmail else {
            "Email format error!".toast_error
            return
        }
        guard let problem = textView.text, problem.count > 0 else {
            "Please tell us your problem.".toast_error
            return
        }
        
        ToastTool.show()
        DispatchQueue.main.asyncAfter(deadline: .now()+2) {
            ToastTool.dismiss()
            self.navigationController?.popViewController(animated: true)
        }
    }
    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }
    
}
